import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany, AfterLoad, OneToOne, JoinColumn
} from "typeorm"
import { Category } from "./Category";
import { User } from "./User";
import { Comment } from "./Comment";

import moment from "moment/moment"
import { Video } from "./Video";

moment.locale('zh-cn')

enum Status {
  DRAFT = 0, // 草稿
  PENDING = 1, // 审核
  PUBLISHED = 2, // 发布
  REJECTED = 3 // 拒绝
}

@Entity()
export class Article {
  @PrimaryGeneratedColumn()
  id: number // 文章id

  @Column({ comment: "文章标题" })
  title: string // 文章标题

  @Column({ type: "longtext", comment: "文章内容" })
  content: string // 文章内容

  @Column({ default: 0, comment: "点赞数" })
  likeCount: number // 点赞数

  @Column({ default: 0, comment: "收藏数" })
  collectCount: number // 收藏数

  @Column({ default: 0, comment: "转发数" })
  shareCount: number // 转发数

  @Column({ type: "enum", enum: Status, default: Status.DRAFT, comment: "发布状态" })
  status: Status // 发布状态

  @ManyToOne(() => User, { createForeignKeyConstraints: false })
  author: User // 作者

  @ManyToOne(() => Category, { createForeignKeyConstraints: false })
  category: Category // 所属类别

  @OneToMany(() => Comment, (comment) => comment.article)
  comment: Comment[] // 拥有的评论

  @Column({ type: "simple-array", comment: "文章封面图片url" })
  coverImages: string[] // 文章封面图片url

  @OneToOne(() => Video, { createForeignKeyConstraints: false })
  @JoinColumn()
  video: Video // 文章视频

  @CreateDateColumn()
  createdAt: Date | string; // 文章发布时间

  @UpdateDateColumn()
  updatedAt: Date | string; // 文章更新时间

  @AfterLoad()
  formatDate() {
    this.createdAt = moment(this.createdAt).format('LL')
    this.updatedAt = moment(this.updatedAt).format('LL')
  }
}